package org.tzw.template.base;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewbinding.ViewBinding;

import com.google.android.material.snackbar.Snackbar;

import org.tzw.template.logger.LocalLogger;
import org.tzw.template.utils.Utils;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import cn.hutool.core.util.ReflectUtil;


public abstract class BaseFragment<T extends ViewBinding> extends Fragment {

    protected T mRoot;
    protected String TAG;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // 调用onCreateViewBinding方法获取binding
        mRoot = onCreateViewBinding(inflater, container);
        TAG = this.getClass().getSimpleName();
        this.onCreateBefore();
        initProperty(getBinding().getRoot());
        this.onCreateAfter();
        return mRoot.getRoot();
    }

    /**
     * 获取布局id
     */
    private T onCreateViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup parent){
        Class<?> aClass = Utils.getGenericClass(this.getClass());
        Method method = ReflectUtil.getMethod(aClass, "inflate", LayoutInflater.class, ViewGroup.class, boolean.class);
        return ReflectUtil.invoke(null, method, inflater, parent, false);
    }


    private void initProperty(View view) {
        initData(view);
        initView(view);
        initListener();
    }
    /**
     * 创建之前操作
     */
    protected void onCreateBefore() {
    }

    /**
     * 创建之后操作
     */
    protected void onCreateAfter() {
    }
    /**
     * 实现控件的监听事件
     */
    protected abstract void initListener();

    /**
     * 初始化控件的方法
     */
    protected abstract void initView(View view);

    /**
     * 初始化变量及数据值
     */
    protected abstract void initData(View view);

    // 子类使用该方法来使用binding
    public T getBinding() {
        return mRoot;
    }

    protected void showToastShort(String message) {
        showToast(message, 0);
    }

    protected void showToastLong(String message) {
        showToast(message, 1);
    }

    /**
     * 吐司信息
     */
    private void showToast(String message, int time) {
        Toast.makeText(getContext(), message, time == 0 ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG).show();
    }

    protected void showSnackBarViewShort(View view, String message) {
        showSnackBarView(view, message, 0);
    }

    protected void showSnackBarViewLong(View view, String message) {
        showSnackBarView(view, message, 1);
    }

    /**
     * SnackBarView
     */
    private void showSnackBarView(View view, String message, int time) {
        Snackbar.make(view, message, time == 0 ? Snackbar.LENGTH_SHORT : Snackbar.LENGTH_LONG).setAction("Action", null).show();
    }
    /**
     * 判断有无网络方法
     * @return
     */
    protected boolean isNetworkAvailable(){
        return Utils.isNetworkAvailable(getContext());
    }

    /**
     * 打印日志方法
     * @param logs
     */
    protected void showLogs(String logs) {
        LocalLogger.i(TAG, logs);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        // 引用置空处理
        mRoot = null;
    }
}
